const chalk = require('chalk')

const LEVEL_DEBUG = 0;
const LEVEL_INFO = 1;
const LEVEL_WARN = 2;
const LEVEL_ERROR = 3;
const LEVEL_FATAL = 4;

function message(level, msg) {
  switch (level) {
    case LEVEL_DEBUG:
      console.log(`${chalk.blue('[DEBUG]')} ${msg}`)
      break;
    case LEVEL_INFO:
      console.log(`${chalk.green.bold('[INFO]')} ${msg}`)
      break
    case LEVEL_WARN:
      console.log(`${chalk.yellow('[WARN]')} ${msg}`)
      break
    case LEVEL_ERROR:
      console.log(`${chalk.red('[ERROR]')} ${msg}`)
      break
    case LEVEL_FATAL:
      console.log(`${chalk.red(msg)}`)
      break
    default:
      console.log(msg)
      break
  }
}

module.exports = {
  level: (process.argv.includes('-d') || process.argv.includes('--debug')) ? LEVEL_DEBUG : LEVEL_INFO,
  log: function (s) {
    message(100, s);
  },
  debug: function (s) {
    if (this.level == LEVEL_DEBUG)
      message(LEVEL_DEBUG, s);
  },
  info: function (s) {
    if (this.level <= LEVEL_INFO)
      message(LEVEL_INFO, s);
  },
  warn: function (s) {
    if (this.level <= LEVEL_WARN)
      message(LEVEL_WARN, s);
  },
  error: function (s) {
    if (this.level <= LEVEL_ERROR)
      message(LEVEL_ERROR, s);
  },
  fatal: function (s) {
    if (this.level <= LEVEL_FATAL)
      message(LEVEL_FATAL, s);
  }
}
